﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using com.codeplex.eatoutadvocate.Engine;
using com.codeplex.eatoutadvocate.DAL;
using System.Configuration;

namespace com.codeplex.eatoutadvocate.BL
{
    /// <summary>
    /// Driver for the recommendation engine
    /// - Can create new recommender
    /// - Build user
    /// - Get list of recommended restaurants based on score
    /// </summary>
    public class Recommender
    {
        private static Recommender recommender = new Recommender();
        public string WeatherAppKey;
        private Recommender() { }         //Private constructor so that class maintains static factory contract

        /// <summary>
        /// Static factory that creates new recommender object
        /// </summary>
        /// <returns>New recommender</returns>
        public static Recommender getNewRecommender()
        {
            return recommender;
        }


        /// <summary>
        /// When user data is supplied, creates an user object to use as a scoring rubric
        /// </summary>
        /// <param name="userTime">Current time</param>
        /// <param name="userMood">Mood</param>
        /// <param name="userMoodLevel">Mood Level</param>
        /// <param name="zipCode">User's location ZipCode</param>
        public void BuildUser(DateTime userTime, 
                            Engine.Mood userMood, 
                            int userMoodLevel, 
                            string zipCode)
        {
            int time = userTime.Hour;                   //TODO Is this 24 hours??
            Engine.Mood mood = userMood;
            int moodLevel = userMoodLevel;
                                                        // Initialize weather object and call 
                                                        //its get method with ZipCode
            
            Weather weather = new Weather();            // Create weather object's get method
                                                        // this will set Weather and 
            weather.AppKey = WeatherAppKey;             // Set App key
            weather.Set(zipCode);
            Engine.Weather weather_enum = weather.GetWeather();                  //Weather Enum Object
            Engine.Temperature temperature_enum = weather.GetTemperature();      //Temp Enum Object

            Score.InitUser(time, mood, moodLevel, weather_enum, temperature_enum);      //Initialize User
        }

        /// <summary>
        /// Update scores for all eat outs
        /// </summary>
        /// <param name="userID">user ID</param>
        /// <returns>List of restaurants</returns>
        public List<string> GetRestaurant(int userID)
        {
            List<Engine.EatOut> eatOuts = DAL.EatOutInfo.get(userID);       //Get all eatouts for the user
            List<string> restaurants;                                       //List to store all restaurants
            for (int i = 0; i < eatOuts.Count; i++)                         //For all eatouts
            {
                eatOuts[i].Score = Score.Compute(eatOuts[i]);               //Compute scores for all eatouts
            }
            restaurants = Engine.Recommendation.Recommend(eatOuts);         //Get recommended list of restaurants
                                                                            //based on score
            return restaurants;                                             //Return list of recommendation
        }
    }
}
